﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace RegistCommon
{
    public class EncryptionHelper
    {
        string encryptionKeyA = "hjznsoft";
        string encryptionKeyB = "BIM_Work";
        private string md5Begin = "Hello";
        private string md5End = "hjwn";
        private string encryptionKey = string.Empty;
        public EncryptionHelper()
        {
            this.InitKey(EncryptionKeyEnum.KeyA);
        }
        public EncryptionHelper(EncryptionKeyEnum key)
        {
            this.InitKey(key);
        }
        private void InitKey(EncryptionKeyEnum key = EncryptionKeyEnum.KeyA)
        {
            if (key != EncryptionKeyEnum.KeyA)
            {
                if (key == EncryptionKeyEnum.KeyB)
                {
                    this.encryptionKey = this.encryptionKeyB;
                }
            }
            else
            {
                this.encryptionKey = this.encryptionKeyA;
            }
        }
        public string EncryptString(string str)
        {
            return this.Encrypt(str, this.encryptionKey);
        }
        public string DecryptString(string str)
        {
            return this.Decrypt(str, this.encryptionKey);
        }
        public string GetMD5String(string str)
        {
            str = this.md5Begin + str + this.md5End;
            MD5 mD = new MD5CryptoServiceProvider();
            byte[] bytes = Encoding.Unicode.GetBytes(str);
            byte[] array = mD.ComputeHash(bytes);
            string text = string.Empty;
            byte[] array2 = array;
            for (int i = 0; i < array2.Length; i++)
            {
                byte b = array2[i];
                text += b.ToString("x2");
            }
            return text;
        }
        private string Encrypt(string str, string sKey)
        {
            DESCryptoServiceProvider dESCryptoServiceProvider = new DESCryptoServiceProvider();
            byte[] bytes = Encoding.Default.GetBytes(str);
            dESCryptoServiceProvider.Key = Encoding.ASCII.GetBytes(sKey);
            dESCryptoServiceProvider.IV = Encoding.ASCII.GetBytes(sKey);
            MemoryStream memoryStream = new MemoryStream();
            CryptoStream cryptoStream = new CryptoStream(memoryStream, dESCryptoServiceProvider.CreateEncryptor(), CryptoStreamMode.Write);
            cryptoStream.Write(bytes, 0, bytes.Length);
            cryptoStream.FlushFinalBlock();
            StringBuilder stringBuilder = new StringBuilder();
            byte[] array = memoryStream.ToArray();
            for (int i = 0; i < array.Length; i++)
            {
                byte b = array[i];
                stringBuilder.AppendFormat("{0:X2}", b);
            }
            stringBuilder.ToString();
            return stringBuilder.ToString();
        }
        private string Decrypt(string pToDecrypt, string sKey)
        {
            DESCryptoServiceProvider dESCryptoServiceProvider = new DESCryptoServiceProvider();
            byte[] array = new byte[pToDecrypt.Length / 2];
            for (int i = 0; i < pToDecrypt.Length / 2; i++)
            {
                int num = Convert.ToInt32(pToDecrypt.Substring(i * 2, 2), 16);
                array[i] = (byte)num;
            }
            dESCryptoServiceProvider.Key = Encoding.ASCII.GetBytes(sKey);
            dESCryptoServiceProvider.IV = Encoding.ASCII.GetBytes(sKey);
            MemoryStream memoryStream = new MemoryStream();
            CryptoStream cryptoStream = new CryptoStream(memoryStream, dESCryptoServiceProvider.CreateDecryptor(), CryptoStreamMode.Write);
            cryptoStream.Write(array, 0, array.Length);
            cryptoStream.FlushFinalBlock();
            StringBuilder stringBuilder = new StringBuilder();
            return Encoding.Default.GetString(memoryStream.ToArray());
        }
    }
}
